package com.itheima.day06.extra;

import java.util.Scanner;

public class State implements Login, Used, Locked {


    @Override
    public void locked(ATM content) {
        System.out.println("account locked!");
    }

    @Override
    public void loadMoney(ATM content) {
        Scanner scanner = new Scanner(System.in);
        int ge;
        int shi;
        int num;
        do {
            System.out.println("please input your number 100-1000");
            num = Integer.parseInt(scanner.nextLine());
            ge = num % 10;
            shi = num / 10 % 10;
        } while (!(ge == 0 && shi == 0 && num <= 1000 && num >= 100));
        if (content.cash > num) {
            content.cash -= num;
            System.out.println("取钱成功！余额还有：" + content.cash);
        } else {
            System.out.println("余额不够！");
        }
        int select = 0;
        do {
            System.out.println("继续取钱请输入1，离开输入2");
            select = Integer.parseInt(scanner.nextLine());
        } while (select != 2 && select != 1);
        switch (select) {
            case 1:
                loadMoney(content);
                break;
            case 2:
                content.status = ATMstatus.LOCKEDOOUT;
                System.out.println("欢迎下次再来");
                break;
        }
    }

    @Override
    public void saveMoney(ATM content) {

    }

    @Override
    public void login(ATM content) {
        Scanner scanner = new Scanner(System.in);
        if (content.status == ATMstatus.LOGIN) {
            System.out.println("username:");
            content.username = scanner.nextLine();
            System.out.println("password:");
            content.password = scanner.nextLine();
            if ("root".equals(content.username) && "root".equals(content.password)) {
                content.status = ATMstatus.USED;
                loadMoney(content);
            } else {
                content.passwordErrorTimes++;
                if (content.passwordErrorTimes >= 3) {
                    content.status = ATMstatus.LOCKED;
                    locked(content);
                } else {
                    System.out.println("错误次数" + content.passwordErrorTimes);
                    login(content);
                }
            }
        }
    }
}
